package com.allianz.webapps.controllers;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.allianz.webapps.https.interfaces.appBeneficiaries.AppBeneficiaries;
import com.allianz.webapps.https.interfaces.appBeneficiaries.pojo.BeneficiariesModel;
import com.allianz.webapps.orderspojo.App;
import com.allianz.webapps.orderspojo.AppBase;
import com.fasterxml.jackson.databind.ObjectMapper;


//http://localhost:8080/pad_marketing/main/getBeneficiaries.action  获取
//http://localhost:8080/pad_marketing/main/saveBeneficiaries.action  保存
@Controller
@RequestMapping(value="main")
public class AppBeneficiariesController {

	@Autowired
	private ObjectMapper objectMapper;
	@Autowired
	private AppBeneficiaries appBeneficiaries;
	
	/**
	 * 获取的请求
	 */
	@RequestMapping(value="getBeneficiaries")
	@ResponseBody
	public BeneficiariesModel getBeneficiaries(@RequestParam(value="jsonParam") String jsonParam,HttpServletRequest request,HttpServletResponse response)
			throws Exception{
		AppBase appBase=(AppBase)objectMapper.readValue(jsonParam, AppBase.class);
		String body=appBeneficiaries.requestGetParam(appBase);
		JSONObject json=new JSONObject(body);
		BeneficiariesModel beneModel=null;
		String body1="";
		if (json.get("Body").equals(null)) {
			if(!("S").equals(json.getString("Status")))
				throw new Exception(json.getString("ErrMsg"));
			else
				return null;
		}else{
			body1=json.getString("Body");
			beneModel=objectMapper.readValue(body1, BeneficiariesModel.class);
		}
		return beneModel;
	}
	
	/**
	 * 保存的请求
	 */
	@RequestMapping(value="saveBeneficiaries")
	@ResponseBody
	public void saveBeneficiaries(@RequestParam(value="jsonParam") String jsonParam,HttpServletRequest request,HttpServletResponse response)
			throws Exception{
		App app=(App)objectMapper.readValue(jsonParam, App.class);
		String body=appBeneficiaries.requestSaveParam(app);
		JSONObject json=new JSONObject(body);
		if(!("S").equals(json.getString("Status")))
			throw new Exception(json.getString("ErrMsg"));
		
	}
	
	
}
